﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

namespace ArbolandiaBL
{

    internal class ImageProvider
    {

        private ArbolandiaDBDataContext _db;

        public ImageProvider()
        {
            _db = new ArbolandiaDBDataContext();
        }
        internal void InsertUpdateImage(Picture pic)
        {
            if (pic.LinkUrl != null && pic.LinkUrl != "")
            {
                pic.LinkUrl = pic.LinkUrl.StartsWith("http://") ? pic.LinkUrl : ("http://" + pic.LinkUrl);
            }
            var query = _db.Picture.Where(w => w.GuidId == pic.GuidId);
            if (query.Count() > 0)
            {
                query.First().GroupdId = pic.GroupdId;
                query.First().IsEnabled = pic.IsEnabled;
                query.First().IsFullSize = pic.IsFullSize;
                query.First().LinkUrl = pic.LinkUrl;
                query.First().MimeType = pic.MimeType;
                query.First().ToolTip = pic.ToolTip;
                query.First().Url = pic.Url;
            }
            else
            {
                _db.Picture.InsertOnSubmit(pic);
            }
            _db.SubmitChanges();
        }
        internal void DeleteImage(string guidid)
        {
            var guid = new Guid(guidid);
            var query = _db.Picture.Where(w => w.GuidId == guid);
            if (query.Count() >= 0)
            {
                _db.Picture.DeleteOnSubmit(query.First());
                _db.SubmitChanges();
            }

        }
        internal void SaveRezizeImage(string fullpath, Stream streamImage, Int32 height, Int32 width)
        {

            try
            {
                System.Drawing.Image imageToBeResized = System.Drawing.Image.FromStream(streamImage);
                int imageHeight = imageToBeResized.Height;
                int imageWidth = imageToBeResized.Width;
                ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Jpeg);
                System.Drawing.Imaging.Encoder myEncoder =
                                System.Drawing.Imaging.Encoder.Quality;
                EncoderParameters myEncoderParameters = new EncoderParameters(1);
                EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 100L);
                myEncoderParameters.Param[0] = myEncoderParameter;
                Bitmap bitmap = new Bitmap(width, height);

                using (Graphics gr = Graphics.FromImage(bitmap))
                {
                    gr.SmoothingMode = SmoothingMode.Default;
                    gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
                    //gr.DrawImage(imageToBeResized, new Rectangle(0, 0, height, width));
                    System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, width, height);
                    gr.DrawImage(imageToBeResized, rectDestination, 0, 0, imageWidth, imageHeight, GraphicsUnit.Pixel);
                }
                //TODO Remove
                ArbolandiaBL.LogError.WriteAppLog("Before Save" + " path : " + fullpath, System.Reflection.MethodBase.GetCurrentMethod().Name);

                bitmap.Save(fullpath, jgpEncoder, myEncoderParameters);
            }
            catch (Exception ex)
            {

                ArbolandiaBL.LogError.WriteLogError(ex.Message);
            }



        }
        private ImageCodecInfo GetEncoder(ImageFormat format)
        {

            ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();

            foreach (ImageCodecInfo codec in codecs)
            {
                if (codec.FormatID == format.Guid)
                {
                    return codec;
                }
            }
            return null;
        }
    }
}
